<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>通知管理</title>
    <link rel="stylesheet" type="text/css" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/fonts/line-icons.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/main.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/responsive.css">
    <link rel="stylesheet" type="text/css" href="../assets/plugins/datatables/bootstrap-table.min.css" />
</head>
<body>
<div class="container-fluid">
    <div class="breadcrumb-wrapper row">
        <div class="col-12 col-lg-3 col-md-6">
            <h4 class="page-title">发布通知</h4>
        </div>
        <div class="col-12 col-lg-9 col-md-6">
            <ol class="breadcrumb float-right">
                <li class="active">通知管理</li>
                <li class="active"> / 发布通知</li>
            </ol>
        </div>
    </div>
</div>
<div class="container-fluid">
    <table id="table">

    </table>
</div>
<div class="modal fade" id="groupModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">选择分组</h5>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="sourceUserId" />
                <input type="hidden" id="noticeId" />
                <div class="form-group row">
                    <h5 class="col-sm-3">组别</h5>
                    <div class="col-sm-7">
                        <select class="form-control form-control-lg" id="groupId"></select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <input type="button" class="btn btn-common" id="sureAdd" value="确定"/>
            </div>
        </div>
    </div>
</div>
<script src="../assets/js/jquery-min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/plugins/datatables/bootstrap-table.min.js"></script>
<script src="../assets/plugins/datatables/bootstrap-table-zh-CN.min.js"></script>
<script src="../assets/js/popper.min.js"></script>
<script>
    //项目根路径
    var ctx = window.parent.ctx;
    var user = window.parent.user;
    var par = $("body");
    var pheight = $(window.parent.document).find(".main-content").height();

    $(function(){
        initModal();
        $.ajax({
            type: "post",
            url: ctx+"/userNotice/getByUserId",
            data: {id:user.id},
            success: function (data) {
                $("#table").bootstrapTable('load',data);
                // 父页面自适应子页面高度
                par.height(pheight);
            }
        });

        $("#sureAdd").click(function () {
            var strJson = {"targetUserId":$("#sourceUserId").val(),"groupId":$('#groupId').val(),"sourceUserId":user.id,"isRecently":'0'};
            $.ajax({
                type: "post",
                url: ctx+"/userFriend/agreeInsert",
                data: strJson,
                success: function () {
                    window.location.reload();
                }
            })
            $.ajax({
                type: "post",
                url: ctx+"/notice/deleteById",
                data: {id:$("#noticeId").val()},
                success: function () {
                    window.location.reload();
                }
            })
        });

        $("#table").bootstrapTable({
            pagination: true,
            pageSize: 8,
            pageNumber: 1,
            smartDisplay: true,
            columns: [{
                field: '',
                title: '序号',
                formatter: function(value,row,index){
                    return index+1;
                }
            },
                {
                    field: '',
                    title: '发送人',
                    formatter: function () {
                        return "管理员";
                    }
                },
                {
                    field: 'notice.message',
                    title: '内容'
                },
                {
                    field: 'notice.sendTime',
                    title: '时间'
                },
                {
                    field: 'operate',
                    title: '操作',
                    events: {
                        'click #deleteNotice': function (e, value, row, index) {
                            $.ajax({
                                type: "post",
                                url: ctx + "/notice/deleteById",
                                data: {id: row.notice.id},
                                success: function () {
                                    window.location.reload();
                                }
                            })
                        },
                        'click #noRead': function (e, value,row, index) {
                            $.ajax({
                                type: "post",
                                url: ctx+"/userNotice/update",
                                data: {id: row.id,isRead: 1},
                                success: function () {
                                    window.location.reload();
                                }
                            })
                        },
                        'click #agree': function (e, value, row) {
                            $("#sourceUserId").val(row.notice.sourceUser.id);
                            $("#noticeId").val(row.notice.id);
                            $("#groupModal").modal('show');
                        },
                        'click #notAgree': function (e,value,row) {
                            $.ajax({
                                type: "post",
                                url: ctx+"/userFriend/notAgreeInsert",
                                data: {sourceUserId: row.notice.sourceUser.id,targetUserId: user.id},
                                success: function (){
                                    window.location.reload();
                                }
                            });
                            $.ajax({
                                type: "post",
                                url: ctx+"/notice/deleteById",
                                data: {id:row.notice.id},
                                success: function () {
                                    window.location.reload();
                                }
                            })
                        }
                    },
                    formatter: function(value, row, index){
                        var buttonStr = "";
                        if(row.notice.targetType===2){
                            buttonStr += "<button id='agree' type='button' class='btn btn-success'>同意</button>";
                            buttonStr += " <button id='notAgree' type='button' class='btn btn-danger'>拒绝</button>";
                        }else{
                            if(row.isRead===0){
                                buttonStr += "<button id='noRead' type='button' class='btn btn-success'>未读</button>";
                            }else{
                                buttonStr += "<button type='button' class='btn btn-common'>已读</button>";
                            }
                            buttonStr += " <button id='deleteNotice' type='button' class='btn btn-danger'>删除</button>";
                        }
                        return buttonStr;
                    }
                }],
            onPostBody: function () {
                $(".page-list").remove();
            }
        })
    });
    function initModal(){
        $("#groupId").empty();
        $.ajax({
            type: "post",
            url: ctx+"/group/findGroupNameByUserId",
            xhrFields: {
                withCredentials: true
            },
            success: function(data) {
                for(var i=0;i<data.length;i++){
                    $("#groupId").append("<option value='"+data[i].id+"'>"+data[i].groupName+"</option>");
                }
            }
        })
    }
</script>
</body>
</html>